package com.xgj.ph.servlet;

import com.alibaba.fastjson.JSON;
import com.xgj.ph.dao.CourierDao;
import com.xgj.ph.dao.impl.CourierDaoImpl;
import com.xgj.ph.entity.Courier;
import com.xgj.ph.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courier")
public class CourierServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String action = req.getParameter("action");
    CourierDao courierDao = new CourierDaoImpl();
    switch (action){
      case "selectAll":
        String courierStationName = req.getParameter("courierStationName");
        String courierStationCommunityName = req.getParameter("courierStationCommunityName");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        Map<String,String> map = new HashMap<>();
        map.put("courierStationName",courierStationName);
        map.put("courierStationCommunityName",courierStationCommunityName);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        List<Courier> list = courierDao.selectAll(map);
        int count = courierDao.selectCount(map);
        Map resultMap = new HashMap();
        resultMap.put("list",list);
        resultMap.put("total",count);
        Result result = Result.success(resultMap);
        resp.getWriter().write(JSON.toJSONString(result));
        break;
      case "update":
        int addOrUpdateFlag = Integer.parseInt(req.getParameter("addOrUpdateFlag"));
        String id = req.getParameter("courierStationId");
        int courierStationId = 0;
        if (id != null && !"".equals(id)){
          courierStationId = Integer.parseInt(id);
        }
        String courierStationName1 = req.getParameter("courierStationName");
        String courierStationImage = req.getParameter("courierStationImage");
        String orderCount = req.getParameter("courierStationOrderCount");
        int courierStationOrderCount = 0;
        if (orderCount != null && !"".equals(orderCount)){
          courierStationOrderCount = Integer.parseInt(orderCount);
        }
        String stationStatus = req.getParameter("courierStationStatus");
        int courierStationStatus = 0;
        if (stationStatus != null && !"".equals(stationStatus)){
          courierStationStatus = Integer.parseInt(stationStatus);
        }
        String courierStationProvince = req.getParameter("courierStationProvince");
        String courierStationCity = req.getParameter("courierStationCity");
        String courierStationDistrict = req.getParameter("courierStationDistrict");
        String courierStationAddress = req.getParameter("courierStationAddress");
        String StationCommunityId = req.getParameter("courierStationCommunityId");
        int courierStationCommunityId = 0;
        if (StationCommunityId != null && !"".equals(StationCommunityId)){
          courierStationCommunityId = Integer.parseInt(StationCommunityId);
        }
        String stationSort = req.getParameter("courierStationSort");
        int courierStationSort = 0;
        if (stationSort != null && !"".equals(stationSort)){
          courierStationSort = Integer.parseInt(stationSort);
        }
        Courier courier = new Courier(
                courierStationId,
                courierStationName1,
                courierStationImage,
                courierStationOrderCount,
                courierStationStatus,
                new Date(),
                courierStationProvince,
                courierStationCity,
                courierStationDistrict,
                courierStationAddress,
                courierStationCommunityId,
                courierStationSort
        );
        Result result1 = null;
        if (addOrUpdateFlag ==1){
          int insert = courierDao.insert(courier);
          if (insert == 1){
            result1 = Result.success("添加成功");
          }else {
            result1 = Result.success("添加失败");
          }
        }else {
          int update = courierDao.update(courier);
          if (update == 1){
            result1 = Result.success("修改成功");
          }else {
            result1 = Result.success("修改失败");
          }
        }
        resp.getWriter().write(JSON.toJSONString(result1));
        break;
      case "delete":
        String id1 = req.getParameter("courierStationId");
        int courierStationId1 = 0;
        if (id1 != null && !"".equals(id1)){
          courierStationId1 = Integer.parseInt(id1);
        }
        int delete = courierDao.delete(courierStationId1);
        Result result2 = null;
        if (delete == 1){
          result2 = Result.success("删除成功");
        }else {
          result2 = Result.success("删除失败");
        }
        resp.getWriter().write(JSON.toJSONString(result2));
        break;
      default:break;
    }
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
  }
}
